(() => {
  // 页面刚开始调用请求获取数据

  // todo页面刚开始调用请求获取数据
  let leaseNum = 1;
  //! 住房id
  let ids = location.search.split("?")[1];
  let ele = {};
  ele.zufang_list = $("#zufang_list");
  ele.zanwu = $("#zanwu");
  ele.chahao = $("#chahao");
  ele.btn_que = $("#btn_que");
  ele.modal_prompt = $("#modal-prompt");
  ele.btn_que3 = $("#btn_que3");
  ele.chahao2 = $("#chahao2");
  ele.chahao3 = $("#chahao3");
  ele.chahao4 = $("#chahao4");
  ele.lianzu_top = $("#lianzu_top");
  ele.zupinMoney = $("#zupinMoney");
  ele.zupin = $("#zupin");
  ele.zuping = $("#zuping");
  ele.clare1 = $("#clare1");
  ele.modal_lease = $("#modal-lease");
  ele.modal_estate = $("#modal-estate");
  ele.modal_pay = $("#modal-pay");
  ele.left_jia = $("#left_jia");
  ele.left_jian = $("#left_jian");
  ele.shengyu = $("#shengyu");
  ele.center_num = $("#center_num");
  ele.housre = $("#housre");
  ele.top_lian = $("#top_lian");
  ele.modal_residue = $("#modal-residue");
  ele.tianshu = $("#tianshu");
  ele.zongNum = $("#zongNum");
  ele.oneHome = $("#oneHome");
  ele.title_name = $("#title_name");
  ele.btn_que4 = $("#btn_que4");
  ele.jieshao = $("#jieshao");
  ele.jieshao2 = $("#jieshao2");
  ele.btn_que2 = $("#btn_que2");
  ele.top_hrig = $("#top_hrig");
  ele.top_nav = $("#top_nav");
  ele.body_c = $("#body_c");
  ele.border_b = $("#border_b");
  ele.zzdzj = $("#zzdzj");
  ele.zzdzj2 = $("#zzdzj2");
  ele.top_img = $("#top_img");
  ele.zujinlq = $("#zujinlq");
  ele.switch = $("#switch");
  ele.jinbi_zong = $("#jinbi_zong");
  ele.chanquans = $("#chanquans");
  ele.payjibi = $("#payjibi");
  ele.zongbi = $("#zongbi");
  ele.shengyufang = $("#shengyufang");
  ele.tianshus = $("#tianshus");
  ele.zhekoujia = $("#zhekoujia");
  ele.wenimg = $("#wenimg");
  ele.modal_explian = $("#modal-explian");
  ele.close_explian = $("#close-explian");

  ele.wenimg.onclick = () => {
    openMdl(ele.modal_explian);
  };
  ele.close_explian.onclick = () => {
    closeMdl(ele.modal_explian);
  };
  let list = [
    {
      state: "1",
    },
    {
      state: "2",
    },
    {
      state: "0",
    },
  ];
  let obj = {};
  let gold = "";
  let is_me = false;
  // initList();
  if (sessionStorage.getItem("refresh") == "true") {
    sessionStorage.removeItem("refresh");
    location.reload();
  }
  // !发请求请求数据
  initAsync();

  ele.chahao2.onclick = () => {
    closeMdl(ele.modal_prompt);
  };
  // 点击租凭楼的x号
  ele.clare1.onclick = () => {
    closeMdl(ele.modal_estate);
  };
  // 点击购买整栋楼弹窗的x号
  ele.chahao4.onclick = () => {
    closeMdl(ele.modal_pay);
  };
  // 点击提示内容剩余一天的时候弹出层的x号
  ele.chahao3.onclick = () => {
    closeMdl(ele.modal_residue);
  };
  closeMdl(ele.zujinlq);
  // todo点击租金领取
  ele.zujinlq.onclick = () => {
    location.href = `../page/rent_collection.html?${obj.id}`;
  };
  // todo点击返回箭头跳转能量房产
  ele.top_img.onclick = () => {
    // 刘欣 引导 2023-6-21 19:11:51
    let ugid = guide.getUsersGuideId();
    // 返回到能量房产 从廉租房
    if (~[110].indexOf(ugid)) guide.hideCheap_110();
    // 返回到能量房产 从世纪城
    else if (~[133].indexOf(ugid)) guide.hideCentury_133();
    history.back();
  };
  // 点击开关
  ele.switch.onclick = () => {
    is_me = !is_me;
    initAsync();
  };
  // !产权显示与隐藏
  if (ids == 1 || ids == 2) {
    closeMdl(ele.chanquans);
  }
  // // 租凭整栋楼点击＋按钮
  // ele.left_jia.onclick = () => {
  //   leaseNum++;
  //   ele.center_num.innerHTML = leaseNum;
  //   ele.zongNum.innerHTML = (leaseNum * obj.hire_money).toFixed(2);
  // };
  // // 租凭整栋楼点击-按钮
  // ele.left_jian.onclick = () => {
  //   if (leaseNum <= 1) {
  //     return;
  //   }
  //   leaseNum--;
  //   ele.center_num.innerHTML = leaseNum;
  //   ele.zongNum.innerHTML = (leaseNum * obj.hire_money).toFixed(2);
  // };

  // 点击租凭和购买
  ele.zuping.onclick = () => {
    if (obj.money == 0) {
      // 说明是租凭打开租凭步进器
      openMdl(ele.modal_estate);
    } else {
      ele.payjibi.innerHTML = obj.zk_money;

      // 直接购买房产
      openMdl(ele.modal_pay);
    }
  };
  // 点击租凭整栋楼
  ele.oneHome.onclick = () => {
    zuping({
      id: ids,
      num: leaseNum,
    });
  };
  // 整楼购买确认
  ele.btn_que4.onclick = () => {
    zuping({
      id: ids,
    });
  };
  // 这是整栋楼租凭或者购买的请求的函数
  function zuping(obj) {
    let token = getCookie("token");
    ajax({
      method: "post",
      url: API_PATH.LEASE,
      header: {
        Authorization: token,
      },
      data: obj,
      success: (res) => {
        openTips(res.msg);
        if (res.code == 0) {
          return;
        } else {
          initAsync();
          // 关闭弹框
          closeMdl(ele.modal_pay);
          closeMdl(ele.modal_estate);
        }
      },
      error: (err) => {
        // openTips(err.msg);
      },
    });
  }

  function initAsync() {
    let token = getCookie("token");
    ajax({
      method: "get",
      url: API_PATH.HOMEDETAIL,
      header: {
        Authorization: token,
      },
      data: {
        id: ids,
        is_me: is_me ? "1" : "0",
      },
      success: (res) => {
        gold = res.data.gold;
        obj = res.data.realEstate; //租房信息
        list = res.data.list; //数组
        //todo 介绍信的显示与隐藏
        if (res.use_js == 1) {
          openMdl(ele.jieshao);
          openMdl(ele.jieshao2);
        } else {
          closeMdl(ele.jieshao);
          closeMdl(ele.jieshao2);
        }
        //!调用模板数据渲染模板
        initList(res.data);
      },
      error: (err) => {
        // openTips(err.msg);
      },
    });
  }

  function initList(dataObj) {
    ele.zongbi.innerHTML = gold;
    ele.jinbi_zong.innerHTML = gold;
    if (list.length == 0) {
      closeMdl(ele.zufang_list);
      openMdl(ele.zanwu);
    } else {
      closeMdl(ele.zanwu);
      openMdl(ele.zufang_list);
    }

    let newList = list
      .map((item, index) => {
        return `
      <div class="son_list">
        <div class="left">
          <div class="num">${item.l_id}</div>
          <div class="touxiang">
            <img style="display:${
              item.info && item.info.head_pic ? "block" : "none"
            };" src="${
          item.info && item.info.head_pic
            ? IMG_DOMAIN + item.info.head_pic
            : "http://img.qxgs.cc/tiantianlaishouzu/img/low_Ent_ousing/touxiang2.png"
        }" alt="" />
            <img style="display:${
              item.info && !item.info.head_pic ? "block" : "none"
            };" src="${
          item.info && item.info.sex == 1
            ? "http://img.qxgs.cc/tiantianlaishouzu/img/my/tx1.png"
            : "http://img.qxgs.cc/tiantianlaishouzu/img/my/tx2.png"
        }" alt="" />
            <img style="display:${
              !item.info ? "block" : "none"
            };" src="http://img.qxgs.cc/tiantianlaishouzu/img/low_Ent_ousing/tou.png" alt="" />
          </div>
          <div class="cen_title" >
            <div class="name" style="display:flex;"><span class="name_fang">房东：</span><span class="name_ming">${
              item.info && item.info.nick_name
                ? item.info.nick_name
                : "天天来收租"
            }</span><sapn style="font-size:10px;color:#ff0000;flex:2;margin-left:10px;display:${
          item.is_in == 1 && item.is_z == 1 && item.my_type == 1
            ? "block"
            : "none"
        };">直管中</sapn><sapn style="font-size:10px;color:#ff0000;margin-left:10px;display:${
          item.is_in == 1 && item.is_z == 0 && item.my_type == 1
            ? "block"
            : "none"
        };">间管中</sapn></div>
            <div class="residue">
              <span>入住人数：</span><span class="residue_num">${
                obj.zf_num - item.lost_house == 0
                  ? '<span style="color:#ff0000;">已住满</span>'
                  : `${item.lost_house}/${obj.zf_num}`
              }</span>
            </div>
            <div class="zhi">
              <span>本楼豪华值：</span><span class="zhi_num">${
                item.luxury_num
              }</span>
            </div>
            <div id="chanquan" style="display:${
              item.over_day ? "block" : "none"
            };" data-id="${item.over_day}" class="chan">
              <span>产权：</span
              ><span id="chan_num"  class="chan_num"></span>
            </div>
          </div>
        </div>
        <div  class="right">
          <!-- 可有可无的租住 -->
          
      <div class="btn_zuzhu">
           <div class="monery" style="display:${
             item.is_in == 0 && ids != 1 ? "block" : "none"
           };">
              <img src="http://img.qxgs.cc/tiantianlaishouzu/img/public/jinbi.png" alt="" />
              <span >${obj.zf_money}</span>
           </div>
        <div id="monery_btn" data-time="${item.over_day}" data-id="${
          item.is_in
        }" data-l_id="${item.l_id}" data-h_id="${item.h_id}" class="${
          item.is_in == 0 ? "monery_btn" : "monery_btn2"
        }">${item.is_in == 0 ? "租住" : "进入"}</div>
        <div id="ruzhu_btn" style=" display:${
          item.is_in == 1 && item.is_z == 0 && item.my_type == 1
            ? "block"
            : "none"
        };margin-top:5px;" data-id="${item.is_in}" data-ids="${
          item.id
        }" data-l_id="${item.l_id}" data-h_id="${
          item.h_id
        }" class="${"monery_btn2"}">入住</div>
      </div>
          <!-- 可有可无的到期 -->
          <div id="tabs_biao" " data-id="${
            item.over_day
          }" class="tabs_biao"><p>快到期</p></div>
        </div>
      </div>
      `;
      })
      .join(" ");
    ele.zufang_list.innerHTML = newList;
    // 渲染名字
    ele.title_name.innerHTML = obj.name;

    // !调用租房逻辑
    house();
    // !判断有没有租金领取
    if (dataObj.has_zu == 0 || ids == 1) {
      closeMdl(ele.zujinlq);
    } else {
      openMdl(ele.zujinlq);
    }
    // !倒计时效果
    let chan_numList = document.querySelectorAll("#chanquan");
    chan_numList.forEach((ite) => {
      let statTime = setInterval(() => {
        if (ite.dataset.id * 1000 <= +new Date()) {
          clearInterval(statTime);
          closeMdl(ite);
          return;
        }
        ite.innerHTML = `  <span>产权：</span
        ><span id="chan_num"  class="chan_num">${getCountTimeDay(
          ite.dataset.id
        )}</span>`;
      }, 1000);
    });
    // !快到期显示隐藏
    let tabs_biaoList = document.querySelectorAll("#tabs_biao");
    tabs_biaoList.forEach((item) => {
      if (ids == 1) {
        closeMdl(item);
        return;
      }
      let now = +new Date();
      let tim = item.dataset.id * 1000;
      const count = (tim - now) / 1000;
      let day = parseInt(count / 60 / 60 / 24);
      console.log(day);
      if (day <= 0) {
        openMdl(item);
      } else {
        closeMdl(item);
      }
    });

    //! 判断是否房东隐藏或者显示上面的租凭
    if (ids != 2) {
      ele.lianzu_top.classList.remove("lianzu_top");
      ele.lianzu_top.classList.add("lianzu_top2");
    }
    if (obj.is_has == 0) {
      if (ids == 1) {
        ele.lianzu_top.style.display = "none";
        // 获取上方高度
        let top_hrig = ele.top_hrig.offsetHeight;
        let border_b = ele.border_b.offsetHeight;
        //  动态设置下面的ui高度
        ele.body_c.style.height = border_b - top_hrig + "px";
      } else {
        // 获取上方高度
        let top_hrig = ele.top_hrig.offsetHeight;
        let border_b = ele.border_b.offsetHeight;
        //  动态设置下面的ui高度
        ele.body_c.style.height = border_b - top_hrig + "px";
        ele.lianzu_top.style.display = "block";
        ele.zupinMoney.innerHTML = obj.hire_house;
      }
    } else {
      // 获取上方高度
      let top_hrig = ele.top_hrig.offsetHeight;
      let border_b = ele.border_b.offsetHeight;
      //  动态设置下面的ui高度
      ele.body_c.style.height = border_b - top_hrig + "px";
      ele.lianzu_top.style.display = "none";
    }
    // !判断是否是购买和租赁
    if (obj.lose_zu) {
      // 说明是租赁
      closeMdl(ele.shengyu);
      ele.zupin.innerHTML = "租赁";
      ele.zupinMoney.innerHTML = obj.hire_money;
    } else {
      // 说明是购买
      ele.shengyu.innerHTML = `剩余房产${obj.lose_buy ? obj.lose_buy : "0"}套`;
      ele.shengyufang.innerHTML = `当前剩余房产：${
        obj.lose_buy ? obj.lose_buy : "0"
      }套`;
      if (obj.now) {
        ele.zhekoujia.innerHTML = `<span>原价${obj.money}</span><span>现价折扣${obj.now}%只需${obj.zk_money}金币</span>`;
      }
      if (obj.now && obj.now == 100) {
        closeMdl(ele.zhekoujia);
      }
      openMdl(ele.shengyu);
      ele.zupin.innerHTML = "购买";
      ele.zupinMoney.innerHTML = obj.money;
    }

    // 租房步进器渲染
    ele.tianshu.innerHTML = `租金：${obj.hire_money}金币/天`;
    ele.zongNum.innerHTML = `${obj.hire_money * 2}`;
    ele.tianshus.innerHTML = `拥有金币：<sapn style="color:#ff0000;">${gold}</span>`;
    // 小时渲染

    ele.housre.innerHTML = obj.power;
    // 世纪城列表为空隐藏上方能量
    // if (list.length == 0) {
    //   closeMdl(ele.top_lian);
    // }
  }

  // !租房逻辑
  function house() {
    // !这是弹窗里面的价格
    ele.zzdzj2.innerHTML = obj.zf_money;
    ele.zzdzj.innerHTML = obj.zf_money;
    //点进入租房提示
    let btnAll = document.querySelectorAll("#monery_btn");
    btnAll.forEach((item) => {
      item.onclick = (e) => {
        console.log(
          ids,
          e.target.dataset.id,
          e.target.dataset.l_id,
          e.target.dataset.h_id
        );
        // 廉租房
        if (ids == 1) {
          // 代表租住
          if (e.target.dataset.id == 0) {
            console.log(e.target.dataset.l_id, e.target.dataset.h_id, "00000");
            lian({
              l_id: e.target.dataset.l_id,
              h_id: e.target.dataset.h_id,
            });
            // 刘欣 引导 租住房间 2023-6-21 13:39:35
            let ugid = guide.getUsersGuideId();
            // 廉租房
            if (~[102].indexOf(ugid)) guide.hideCheap_102();
          } else {
            // 刘欣 引导 进入房间 2023-6-21 13:39:35
            let ugid = guide.getUsersGuideId();
            if (ugid == 104) guide.hideCheap_104();
            // 代表进入
            location.href = `../page/low_Ent_ousing_home.html?${e.target.dataset.l_id}&${e.target.dataset.h_id}`;
          }
        } else {
          let now = +new Date();
          let tim = e.target.dataset.time * 1000;
          const count = (tim - now) / 1000;
          let day = parseInt(count / 60 / 60 / 24);
          // todo点击确认租房后打开提示框
          if (e.target.dataset.id == 0) {
            // 刘欣 引导 租住房间 2023-6-21 21:42:44
            let ugid = guide.getUsersGuideId();
            // 世纪城
            if (~[122, 123].indexOf(ugid)) guide.hideCentury_122();
            // !表示是需要购买的
            if (ids == 3 || ids == 4) {
              // *表示d大于一天
              openMdl(ele.modal_prompt);
              // todo点击确认租房发请求
              ele.btn_que3.onclick = () => {
                houseAnyc({
                  l_id: e.target.dataset.l_id,
                  h_id: e.target.dataset.h_id,
                });
              };
            }
            // 世纪城
            else {
              // 刘欣 引导 确认租住 2023-6-23 08:14:34
              let ugid = guide.getUsersGuideId();
              if (~[122, 123].indexOf(ugid)) {
                console.log("不弹窗提示租期");
                day = 100;
              }
              if (day >= 1) {
                // *表示d大于一天
                openMdl(ele.modal_prompt);
                // todo点击确认租房发请求
                ele.btn_que3.onclick = () => {
                  houseAnyc({
                    l_id: e.target.dataset.l_id,
                    h_id: e.target.dataset.h_id,
                  });
                  // 刘欣 引导 确认租住 2023-6-22 13:26:34
                  // let userGuideId = getItem("userGuideId");
                  // if (~[123].indexOf(userGuideId)) guide.hideCentury_123();
                };
              } else {
                // *表示不足一天
                openMdl(ele.modal_residue);
                // todo点击确认租房发请求
                ele.btn_que2.onclick = () => {
                  houseAnyc({
                    l_id: e.target.dataset.l_id,
                    h_id: e.target.dataset.h_id,
                  });
                };
              }
            }
          } else {
            // 刘欣 引导 进入房间 2023-6-22 13:39:25
            let ugid = guide.getUsersGuideId();
            if (~[124].indexOf(ugid)) guide.hideCentury_124();
            // !表示直接进入房间
            location.href = `../page/low_Ent_ousing_home.html?${e.target.dataset.l_id}&${e.target.dataset.h_id}`;
          }
        }
      };
    });
    //! 房东点击入住
    let ruzhu_btnAll = document.querySelectorAll("#ruzhu_btn");
    ruzhu_btnAll.forEach((item) => {
      item.onclick = () => {
        fangdong(item.dataset.l_id, item.dataset.h_id, item.dataset.ids);
      };
    });
  }

  //! 房东点击入住
  function fangdong(l_id, h_id, id) {
    let token = getCookie("token");
    ajax({
      method: "post",
      url: API_PATH.FNAGHOMEISJIN,
      header: {
        Authorization: token,
      },
      data: {
        id: id,
      },
      success: (res) => {
        openTips(res.msg);
        if (res.code == 1) {
          location.href = `../page/low_Ent_ousing_home.html?${l_id}&${h_id}`;
        }
      },
      error: (err) => {
        // openTips(err.msg);
      },
    });
  }
  //! 廉租房租房
  function lian(data) {
    let token = getCookie("token");
    ajax({
      method: "post",
      url: API_PATH.ZUHOUSE,
      header: {
        Authorization: token,
      },
      data: data,
      success: (res) => {
        openTips(res.msg);
        // 刷新页面
        initAsync();
      },
      error: (err) => {
        // openTips(err.msg);
      },
    });
  }
  // todo租房请求
  function houseAnyc(data) {
    let token = getCookie("token");
    ajax({
      method: "post",
      url: API_PATH.ZUHOUSE,
      header: {
        Authorization: token,
      },
      data: data,
      success: (res) => {
        console.log(res);
        openTips(res.msg);
        // 关闭弹出层
        closeMdl(ele.modal_prompt);
        closeMdl(ele.modal_residue);
        // 刘欣 引导 租住世纪城成功
        let ugid = guide.getUsersGuideId();
        if (~[123].indexOf(ugid) && res.code == 1) guide.hideCentury_123();
        // -------------------------
        // 刷新页面
        initAsync();
      },
      error: (err) => {
        // openTips(err.msg);
      },
    });
  }
})();
